package com.structure.semaphore;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.concurrent.Semaphore;

/**
 * @Author Tanyu
 * @Date 2020/6/10 17:06
 * @Description
 * @Version 1.0
 */
public class Customer implements Runnable {

  private Semaphore washbasin;
  private String name;

  public Customer(Semaphore washbasin, String name) {
    this.washbasin = washbasin;
    this.name = name;
  }

  @Override
  public void run() {

    try {
      SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");
      Random random = new Random();

      washbasin.acquire();
      System.out.println(sdf.format(new Date()) + " " + name + "开始洗手。。。");
      Thread.sleep((long) ((random.nextDouble() * 5000) + 2000));
      System.out.println(sdf.format(new Date()) + " " + name + "洗手完毕。。。");
      washbasin.release();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }

  }
}
